package com.baomidou.springwind.controller.city;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.WeekOrder;
import com.baomidou.springwind.entity.WeekOrder;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.entity.Project;
import com.baomidou.springwind.enums.NodeTypeEnum;
import com.baomidou.springwind.pojo.DrivingRoute;
import com.baomidou.springwind.pojo.Test;
import com.baomidou.springwind.service.WeekOrderService;
import com.baomidou.springwind.service.ProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 城市配送:每周订单量
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/city")
public class WeekOrderController extends BaseController {

    @Autowired
    private WeekOrderService weekOrderService;
    @Autowired
    private ProjectService projectService;
    /**
     * 城市数据导入--客户分析数据--显示医院
     */
    @RequestMapping("/weekorder/list")
    @Permission("1001")
    public String customList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/week_order_list", model);
    }

    /**
     * 城市数据导入--客户分析数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/weekorder/getData")
    @Permission("1001")
    public String getData(Page<WeekOrder> page,String batchId) {
        page = new Page<WeekOrder>(page.getCurrent(), page.getSize());
        EntityWrapper<WeekOrder> wrapper = new EntityWrapper<WeekOrder>();
        if (StringUtils.isNotEmpty(batchId)){
            wrapper.eq("batch_id",batchId);
        }
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = weekOrderService.selectPage(page, wrapper);
        List<WeekOrder> weekOrderList = page.getRecords();
        if (weekOrderList.size() > 0) {
            Project project = projectService.selectById(weekOrderList.get(0).getBatchId());
            for (WeekOrder weekOrder : weekOrderList) {
                weekOrder.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }

    /**
     * 客户分析主界面
     */
    @RequestMapping("/weekorder")
    @Permission("1001")
    public String customGroup(Model model) {
        return pjaxPath("/lpp/network/city/flow/main", model);
    }

    /**
     * 物流量分析--各医院每天的洗涤量
     */
    @ResponseBody
    @RequestMapping("/weekorder/bar1")
    @Permission("1001")
    public String  flowBybarClean() throws Exception{
        String hospital=request.getParameter("hospital");
        EntityWrapper<WeekOrder> wrapper = new EntityWrapper<WeekOrder>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        if (hospital==null||hospital==""){
            wrapper.eq("id", MaxHospitalId());
        }
        if (!StringUtils.isBlank(hospital)) {
            wrapper.eq("id", Integer.parseInt(hospital));
        }
        final List<WeekOrder> weekOrders = weekOrderService.selectList(wrapper);
        JSONArray a1 = new JSONArray();
        JSONArray t = new JSONArray();
        JSONObject ret = new JSONObject();
        for (int i=1;i<=6;i++){
            t.add("周"+i);
        }
        a1.add(weekOrders.get(0).getMonday());
        a1.add(weekOrders.get(0).getTuesday());
        a1.add(weekOrders.get(0).getWednesday());
        a1.add(weekOrders.get(0).getThursday());
        a1.add(weekOrders.get(0).getFriday());
        a1.add(weekOrders.get(0).getSaturday());
        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);
    }

    /**
     * 物流量分析--医院洁衣架数量分布图
     */
    @ResponseBody
    @RequestMapping("/weekorder/bar2")
    @Permission("1001")
    public String  flowByDirty() throws Exception{
        int selectCount=6;
        EntityWrapper<WeekOrder> wrapper = new EntityWrapper<WeekOrder>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<WeekOrder> weekOrders = weekOrderService.selectList(wrapper);
        int minCount=0,maxCount=0,difference=0;
        int countlist[]=new int[weekOrders.size()];
        //建立一个数组
        for (int i=0;i<weekOrders.size();i++){
            countlist[i]=(int)((weekOrders.get(i).getMonday()+weekOrders.get(i).getSaturday()+weekOrders.get(i).getFriday()+weekOrders.get(i).getThursday()+weekOrders.get(i).getSaturday()+weekOrders.get(i).getWednesday())/550/30*1.3/2)+1;
        }
        Arrays.sort(countlist);
        maxCount=countlist[countlist.length-1];
        minCount=countlist[0];
        difference=(maxCount-minCount)/selectCount;
        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        for (int i=0;i<selectCount-1;i++){
            t.add("["+(minCount+difference*i)+","+(minCount+difference*(i+1))+"]");
        }
        t.add("["+(maxCount-difference)+","+maxCount+"]");
        //找到a1需要的数据
        JSONArray a1 = new JSONArray();
        int number1=0,number2=0;
        for (int i=0;i<selectCount-1;i++){
            for(int j=0;j<countlist.length;j++){
                if((minCount+difference*i)<=countlist[j]&&countlist[j]<(minCount+difference*(i+1))){
                    number1++;
                }
            }
            a1.add(number1);
            number2=number1+number2;
            number1=0;
        }
        a1.add(countlist.length-number2);
        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);
    }


    /**
     * 物流量分析--每天的洗涤量分布图
     */
    @ResponseBody
    @RequestMapping("/weekorder/bar3")
    @Permission("1001")
    public String  flowBybarCount() throws Exception{
        EntityWrapper<WeekOrder> wrapper = new EntityWrapper<WeekOrder>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<WeekOrder> weekOrders = weekOrderService.selectList(wrapper);
        int countlist[]=new int[6];
        //建立一个数组
        countlist[0]=0;
        countlist[1]=0;
        countlist[2]=0;
        countlist[3]=0;
        countlist[4]=0;
        countlist[5]=0;
        for (int i=0;i<weekOrders.size();i++){
            countlist[0]=countlist[0]+weekOrders.get(i).getMonday();
            countlist[1]=countlist[1]+weekOrders.get(i).getTuesday();
            countlist[2]=countlist[2]+weekOrders.get(i).getWednesday();
            countlist[3]=countlist[3]+weekOrders.get(i).getThursday();
            countlist[4]=countlist[4]+weekOrders.get(i).getFriday();
            countlist[5]=countlist[5]+weekOrders.get(i).getSaturday();
        }

        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        for (int i=1;i<=6;i++){
            t.add("周"+i);
        }
        //找到a1需要的数据
        JSONArray a1 = new JSONArray();
        for (int i=0;i<6;i++){
            a1.add(countlist[i]);
        }
        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);

    }
    private long MaxHospitalId(){
        EntityWrapper<WeekOrder> wrapper1 = new EntityWrapper<WeekOrder>();
        wrapper1.eq("operator_id", getCurrentUserId());
        wrapper1.orderBy("create_time", false);
        wrapper1.eq("batch_id", getProjectId());
        WeekOrder weekOrder = weekOrderService.selectOne(wrapper1);
        return  weekOrder.getId();
    }
}
